package com.gary.springboot.controller;

import com.gary.springboot.entities.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author pengrui
 * @date 2020/11/19 - 9:20
 */
@Controller
public class UserController {

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam(" password") String password,
                        HashMap<String, Object> map,
                        HttpSession session) {
        if (StringUtils.hasText(username) && "123456".equals(password)) {
            // 使用重定向 解决刷新页面时 重复提交表单问题
            session.setAttribute("loginUser", username);
            return "redirect:/main.html";
        }
        map.put("msg", "用户名密码错误");
        return "index";
    }

    @PostMapping("/userInfo")
    @ResponseBody
    public String  testVue(UserInfo userInfo, HttpServletResponse response) throws IOException {
        System.out.println(userInfo);
        response.addCookie(new Cookie("pr","value"));
        return "success";
    }
}
